﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;




namespace PuttySessionTool
{


    public partial class frmSessionSync : Form
    {
        CWinscpSession[] WinscpSessions = null;
        CPuttySession[] PuttySessions = null;

        public frmSessionSync()
        {
            InitializeComponent();

        }

        private void btnSettings_Click(object sender, EventArgs e)
        {

        }

        private void btnExecute_Click(object sender, EventArgs e)
        {


        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            CWinscpIniFile wif = new CWinscpIniFile(@"D:\Pros\winscp\winscp.ini");
            wif.LoadFromIniFile();
            WinscpSessions = wif.sessions.ToArray();
            PuttySessions = CPuttySession.GetInstancesFromRegistry();
            SortedList<UInt32, CompareSession> list = new SortedList<uint, CompareSession>();
            UInt32 SortedListCount = 0;
            List<CWinscpSession> w = new List<CWinscpSession>();
            List<CPuttySession> p = new List<CPuttySession>();
            for (int i = 0; i < WinscpSessions.Length; i++)
            {
                w.Add(WinscpSessions[i]);
            }
            for (int i = 0; i < PuttySessions.Length; i++)
            {
                p.Add(PuttySessions[i]);
            }

            //todo 整理
            CompareSession newMatch;
            while ((newMatch = GetNewCompareSession(w, p)) != null)
            {
                list.Add(++SortedListCount, newMatch);
                w.Remove(newMatch.winscp);
                p.Remove(newMatch.putty);
            }

            for (int i = 0; i < w.Count; i++)
            {
                list.Add(++SortedListCount, new CompareSession(w[i], null, CompareSessionFlag.Winscp));
            }

            for (int j = 0; j < p.Count; j++)
            {
                list.Add(++SortedListCount, new CompareSession(null, p[j], CompareSessionFlag.Putty));
            }

            for(int i=1;i<=list.Count;i++)
            {
                listView1.Items.Add(new ListViewItem(list[(UInt32)i].toStringArray()));
            }
        }

        private CompareSession GetNewCompareSession(List<CWinscpSession> w, List<CPuttySession> p)
        {
            CompareSession ret = null;
            for (int i = 0; i < w.Count; i++)
            {
                for (int j = 0; j < p.Count; j++)
                {
                    if (w[i].SessionKey.Equals(p[j].sSessionKey)
                        && w[i].GetTunnel() == null
                        && w[i].GetHostName().Equals(p[j].sHostName)
                        && w[i].GetPortNumber() == p[j].nPort)
                    {
                        ret = new CompareSession(w[i], p[j], CompareSessionFlag.Match);
                        i = w.Count;
                        j = p.Count;
                    }
                }
            }

            if (ret == null)
            {
                for (int i = 0; i < w.Count; i++)
                {
                    for (int j = 0; j < p.Count; j++)
                    {
                        if (w[i].SessionKey.Equals(p[j].sSessionKey))
                        {
                            ret = new CompareSession(w[i], p[j], CompareSessionFlag.MatchSessionKey);
                            i = w.Count;
                            j = p.Count;
                        }
                    }
                }
            }

            if (ret == null)
            {
                for (int i = 0; i < w.Count; i++)
                {
                    for (int j = 0; j < p.Count; j++)
                    {
                        if (w[i].GetTunnel() == null
                            && w[i].GetHostName().Equals(p[j].sHostName)
                            && w[i].GetPortNumber() == p[j].nPort)
                        {
                            ret = new CompareSession(w[i], p[j], CompareSessionFlag.MatchHostNameAndPort);
                            i = w.Count;
                            j = p.Count;
                        }
                    }
                }
            }
            return ret;
        }

        private void SetOperate(ListView lv,int operate)
        {
            for (int i=0;i<lv.SelectedItems.Count;i++)
            {

                lv.SelectedItems[i].SubItems[4].Text = operate == 1 ? "->" : operate == 2 ? "<-" : "--";
            }
        }

        private void tsmiOpMoveToRight_Click(object sender, EventArgs e)
        {
            SetOperate(listView1, 1);
        }

        private void tsmiOpMoveToLeft_Click(object sender, EventArgs e)
        {
            SetOperate(listView1, 2);
        }

        private void tsmiOpNoOperate_Click(object sender, EventArgs e)
        {
            SetOperate(listView1, 3);
        }

        private void btnSelectOperate_Click(object sender, EventArgs e)
        {
            for (int i=0;i<listView1.Items.Count;i++)
            {
                if (listView1.Items[i].SubItems[4].Text.Equals("->") || listView1.Items[i].SubItems[4].Text.Equals("<-") )
                {
                    listView1.Items[i].Checked = true;
                }else{
                    listView1.Items[i].Checked = false;
                }
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            int settingCount = 0;
            bool setting = true;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView1.Items[i].Checked)
                {
                    settingCount ++;
                }
            }
            if (settingCount == listView1.Items.Count) setting = false;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                listView1.Items[i].Checked = setting;
            }
        }

        private void btnInvertSelect_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                listView1.Items[i].Checked = !listView1.Items[i].Checked;
            }
        }

        //private void listView1_ItemActivate(object sender, EventArgs e)
        //{
            
        //    ListView lv = (ListView)sender;
        //    for (int i=0;i<lv.SelectedItems.Count;i++)
        //    {
        //       // lv.SelectedItems[i].SubItems["colOperate"].Text;
        //    }
        //}

        //private void listView1_Click(object sender, EventArgs e)
        //{

        //}

        //private void listView1_MouseClick(object sender, MouseEventArgs e)
        //{
        //    contextMenuStripOperate.Top = 100;
        //    contextMenuStripOperate.Left = 200;
        //    contextMenuStripOperate.Show();
        //}

    }

    enum CompareSessionFlag : int
    {
        Match,
        MatchHostNameAndPort,
        MatchSessionKey,
        Winscp,
        Putty
    }

    class CompareSession
    {
        public CWinscpSession winscp;
        public CPuttySession putty;
        public CompareSessionFlag flag;

        public CompareSession(CWinscpSession winscp, CPuttySession putty, CompareSessionFlag flag)
        {
            this.winscp = winscp;
            this.putty = putty;
            this.flag = flag;
        }

        public string[] toStringArray()
        {
            string opt = "";
            switch ((int)flag)
            {
                case (int)CompareSessionFlag.Match:
                    opt = "=";
                    break;
                case (int)CompareSessionFlag.MatchHostNameAndPort:
                    opt = "H&P";
                    break;
                case (int)CompareSessionFlag.MatchSessionKey:
                    opt = "S";
                    break;
                case (int)CompareSessionFlag.Putty:
                    opt = "+";
                    break;
                case (int)CompareSessionFlag.Winscp:
                    opt = "-";
                    break;
            }
            return new string[]
            {
                putty==null?"":CUtility.SessionKeyToDisplayName(putty.sSessionKey),
                putty==null?"":putty.sHostName,
                putty==null?"":""+putty.nPort,
                opt,
                "",
                winscp==null?"":CUtility.SessionKeyToDisplayName(winscp.SessionKey),
                winscp==null?"":winscp.GetHostName(),
                winscp==null?"":""+winscp.GetPortNumber(),
                winscp==null?"":winscp.GetTunnel()==null?"":winscp.GetTunnel()
            };
        }
    }
}
